package com.beyondprototype.batch.config;

import java.util.Collections;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.MongoDriverInformation;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.internal.MongoClientImpl;

@Configuration
public class MongoConfigration {

	@Bean
	public MongoDatabaseFactory factory() throws Exception {
		
		MongoCredential credential = MongoCredential.createCredential("developer", "demo", "12345".toCharArray());
		
		ServerAddress serverAddress = new ServerAddress("localhost",27017);
		
		MongoDriverInformation info = MongoDriverInformation.builder().build();
		
		MongoClientSettings build = MongoClientSettings.builder()
				.applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(serverAddress)))
				.credential(credential)
				.build();
		
		MongoClient mongoClient = new MongoClientImpl(build, info);
		
		return new SimpleMongoClientDatabaseFactory(mongoClient, "demo");
		
	}
}
